package com.enuo;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class AuthenticationRealm extends AuthorizingRealm
{
//    @Autowired
//    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals)
    {
        Principal principal = (Principal) principals.fromRealm(getName()).iterator().next();
        
        if (principal != null)
        {
            List<String> authorities = new ArrayList<String>();
            
            if (authorities != null)
            {
                authorities.add("admin:product");
                SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
                authorizationInfo.addStringPermissions(authorities);
                
                return authorizationInfo;
            }
        }
 
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException
    {
        UsernamePasswordToken passwordToken = (UsernamePasswordToken) token;
        passwordToken.setPassword("adv".toCharArray());
        passwordToken.setUsername("张三");
        String username = passwordToken.getUsername();
        String password = new String(passwordToken.getPassword());
//
//        if (!Utils.isMobile(username) || Utils.isEmpty(password))
//        {
//            throw new AccountException();
//        }
//
//        User user = userService.findByMobile(username);
//
//        if (null == user)
//        {
//            throw new AccountException();
//        }

//        if (!user.getEnabled())
//        {
//            throw new DisabledAccountException();
//        }

//        if (!user.getPassword().equals(DigestUtils.sha256Hex(password)))
//        {
//            throw new IncorrectCredentialsException();
//        }
System.out.println(getName());
        return new SimpleAuthenticationInfo(new Principal(10L, username), password, getName());
    }
}
